package com.crm.manager.controller;

import com.crm.common.core.domain.R;
import com.crm.manager.domain.Business;
import com.crm.manager.domain.BusinessRecord;
import com.crm.manager.domain.dto.BatchDTO;
import com.crm.manager.domain.dto.BusinessDTO;
import com.crm.manager.domain.dto.BusinessPageDTO;
import com.crm.manager.domain.vo.BusinessVO;
import com.crm.manager.domain.vo.PageVO;
import com.crm.manager.service.BusinessService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/business")
public class BusinessController {

    @Autowired
    private BusinessService businessService;

    /**
     * 增加
     * @param business
     */
    @PostMapping
    public R add(@RequestBody Business business){
        businessService.add(business);
        return R.ok();
    }

    /**
     * 分页查询
     */
    @GetMapping("/list")
    public PageVO page(BusinessPageDTO dto){
        return businessService.pageQuery(dto);
    }

    /**
     * 查询一个商机（客户）信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<BusinessVO> getBusinessDetail(@PathVariable Integer id) {
        return R.ok(businessService.getBusinessDetail(id));
    }

    /**
     * 跟进时，触发的存储逻辑---存detail，存跟进记录
     * @param dto
     * @return
     */
    @PostMapping("/record")
    public R addRecord(@RequestBody BusinessDTO dto){
        businessService.addRecord(dto);
        return R.ok();
    }

    /**
     * 查询商机的跟踪记录
     * @param businessId
     * @return
     */
    @GetMapping("/record/list")
    public R<List<BusinessRecord>> getRecordListById(Integer businessId){
        return R.ok(businessService.getRecordListById(businessId));
    }

    /**
     * 放入公海，其实就是改个状态
     */
    @PutMapping("/back/{id}/{reason}")
    public R moveToPool(@PathVariable Integer id, @PathVariable String reason){
        businessService.moveToPool(id, reason);
        return R.ok();
    }
    /**
     * 公海的分页查询
     */
    @GetMapping("/pool")
    public PageVO poolPageQuery(BusinessPageDTO dto){
        return businessService.poolPageQuery(dto);
    }
    /**
     * 从pool里面捞出来,支持批量
     * 注意，谁捞的，这些商机owner就是谁，并且状态改为待跟进
     */
    @PutMapping("/gain")
    public R gain(@RequestBody BatchDTO dto){
        businessService.gain(dto);
        return R.ok();
    }

    /**
     *分配商机，支持批量
     * 分配就是把owner改为传过来的user
     * @param dto
     * @return
     */
    @PutMapping("/assignment")
    public R assignment(@RequestBody BatchDTO dto){
        businessService.assignment(dto);
        return R.ok();
    }
}
